//
//  CurrentListController.swift
//  ExpressDriver
//
//  Created by sheng on 2017/11/9.
//  Copyright © 2017年 sheng. All rights reserved.
//

import UIKit
import SnapKit
import MJRefresh

class CurrentListController: UIViewController,UITableViewDataSource,UITableViewDelegate, OrderCellDelegate{
    
    @IBOutlet weak var completeLabel: UILabel!
    
    @IBOutlet weak var outstandLabel: UILabel!
    
    @IBOutlet weak var dateSelect: UITextField!
    
    @IBOutlet weak var orderTabview: UITableView!
    
    @IBOutlet weak var cleanderImg: UIButton!
    
    let freshHeader = MJRefreshNormalHeader()
    
    var scrrenHeight = UIScreen.main.bounds.height
    
    var horizontalLine: UIView! //分隔线
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }
    
    
    func initUI()  {
        
        self.cleanderImg.snp.makeConstraints { (make) in
            make.top.equalTo(10)
            make.width.equalTo(25)
            make.height.equalTo(25)
            make.right.equalTo(-15)
        }
        
        self.dateSelect.snp.makeConstraints { (make) in
            make.top.equalTo(10)
            make.width.equalTo(150)
            make.height.equalTo(25)
            make.centerY.equalTo(self.cleanderImg)
            make.right.equalTo(self.cleanderImg.snp.left).offset(-15)
        }
        
        self.completeLabel.snp.makeConstraints { (make) in
            make.top.equalTo(10)
            make.left.equalTo(15)
            make.height.equalTo(12)
        }
        
        self.outstandLabel.snp.makeConstraints { (make) in
            make.top.equalTo(self.completeLabel.snp.bottom).offset(5)
            make.left.equalTo(self.completeLabel)
            make.height.equalTo(12)
        }
        
        self.horizontalLine =  UIView()
        self.horizontalLine.backgroundColor = UIColor.lightGray
        self.view.addSubview(self.horizontalLine)
        self.horizontalLine.snp.makeConstraints { (make) -> Void in
            make.height.equalTo(0.5)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(outstandLabel.snp.bottom).offset(10)
            
        }
        
        self.orderTabview.snp.makeConstraints { (make) in
            make.top.equalTo(self.horizontalLine.snp.bottom)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(scrrenHeight - 30)
        }
        
        freshHeader.setTitle("Pull to refresh", for: .idle)
        freshHeader.setTitle("Release to refresh", for: .pulling)
        freshHeader.setTitle("Loading...", for: .refreshing)
        //header.lastUpdatedTimeLabel.isHidden = true
        freshHeader.setRefreshingTarget(self, refreshingAction: #selector(headerRefresh))
        orderTabview!.mj_header = freshHeader
        
        
        self.orderTabview.dataSource = self
        self.orderTabview.delegate = self
        let nib = UINib(nibName: String(describing: OrderCellTableViewCell.self), bundle: nil)
        self.orderTabview.register(nib, forCellReuseIdentifier: "orderItem")
        self.orderTabview?.estimatedRowHeight = 44.0
        self.orderTabview?.rowHeight = UITableViewAutomaticDimension
        
        
    }
    
    func headerRefresh(){
        print("下拉刷新.")
        sleep(2)
        //重现加载表格数据
        orderTabview!.reloadData()
        //结束刷新
        orderTabview!.mj_header.endRefreshing()
    }
    
    
    
    @IBAction func selectDate(_ sender: UIButton) {
        let alertController:UIAlertController=UIAlertController(title: "\n\n\n\n\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
        // 初始化 datePicker
        let datePicker = UIDatePicker( )
        datePicker.datePickerMode = UIDatePickerMode.date
        datePicker.frame = CGRect(x: 0, y: 0, width: alertController.view.bounds.size.width, height: 230)
        // 设置默认时间
        datePicker.date = NSDate() as Date
        alertController.addAction(UIAlertAction(title: "determine", style: UIAlertActionStyle.default){
            (alertAction)->Void in
            
            let dformatter = DateFormatter()
            dformatter.dateFormat = "yyyy-MM-dd"
            // 使用日期格式器格式化日期、时间
            let datestr = dformatter.string(from: datePicker.date)
            self.dateSelect.text = datestr
            print("date select: \(datestr)")
        })
        alertController.addAction(UIAlertAction(title: "cancel", style: UIAlertActionStyle.cancel,handler:nil))
        alertController.view.addSubview(datePicker)
        self.present(alertController, animated: true, completion: nil)
        
    }
    
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "orderItem", for: indexPath) as! OrderCellTableViewCell
        cell.selectionStyle = .none
        cell.delegate = self
        return cell
    }
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        print("第\(indexPath.row)行被点击了")
    }
    
    
    
    func viewOrderDetail(label: UILabel) {
    
        let orderController = OrderDetailController();
        orderController.title = "Detail"
        self.navigationController?.pushViewController(orderController, animated: true)
    }
    
    
    func printOrder(label: UILabel) {
        let printController = PrintController();
        printController.title = "Print"
        self.navigationController?.pushViewController(printController, animated: true)
    }
    
    
    func signatureOrder(label: UILabel) {
        let signatureController = SignatureController()
        signatureController.title = "Signature"
        self.navigationController?.pushViewController(signatureController, animated: true)
    }

    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    
    
}
